local vyVer = "0.5";
local vyCList = {};
local GameTooltip_obj = GameTooltip;
local GameTooltipHyperLink_obj = GameTooltipHyperLink;
local vyCards = {};

function vyParseCards()
	local vyCSum, vyCHave;
	vyCSum = 0;
	vyCHave = 0;
	for i = 0 , 16 do
		local cnt = LuaFunc_GetCardMaxCount(i);
		if( cnt ~= nil and cnt > 0 ) then
			for j = 1 , cnt do
				local CardID,CardFlag,CardName,CardTip = LuaFunc_GetCardInfo(i , j - 1);
				local vyhex = dec2hex(CardID);
				vyCList[vyhex] = {};
				vyCList[vyhex]["Flag"] = CardFlag;
				vyCList[vyhex]["Tip"] = CardTip;
				vyCSum = vyCSum + 1;
				if (CardFlag == 1) then
					vyCHave = vyCHave + 1;
					local mobname = string.gsub(CardName, "Card -", "")
					vyCards[mobname] = true;
				end
			end
		end
	end
	DEFAULT_CHAT_FRAME:AddMessage("There are |cffff0000"..vyCSum.."|r cards in total, you have |cffff0000"..vyCHave.."|r.");
end

function vyCardCheck(tooltip, link)
	if (link == nil) then 
		return false;
	end
	local _, _, vyhex = string.find(link, ':([0-9a-f]+)');
	if (vyCList[vyhex] == nil) then 
		return false;
	end
	tooltip:AddLine(vyCList[vyhex]["Tip"]);
	if (vyCList[vyhex]["Flag"] == 0) then
		tooltip:AddLine("|cffff0000You don't have this card.|r");
	else
		tooltip:AddLine("|cff00ff00You already have this card.|r");
	end
end

function vyCardInfo_OnEvent(event)
	if (event == "CARDBOOKFRAME_UPDATE") then
		vyParseCards();
	end
	if (event == "UPDATE_MOUSEOVER_UNIT") then
		vyMouseOverUnit();
	end
end

function vyMouseOverUnit()
	_G["vyCardToolTip"] = nil;
	if (not UnitCanAttack("player", "mouseover") or UnitIsPlayer("mouseover")) then
		return false;
	end
	local name = UnitName("mouseover");
	if (vyCards[name] == true) then
		_G["vyCardToolTip"] = "CardInfo: |cff00ff00You have the card|r";
	else
		_G["vyCardToolTip"] = "CardInfo: |cffff0000You don't have the card|r";
	end
end

function vyCardInfo_OnUpdate(this) 
--	if (os.difftime(os.time(), ttUpdate) > 1 and vyToolTip == true) then
--		vyToolTip = false;
--		GameTooltip:AddLine("vy: " .. vyMobToolTip, 1, 1, 1);
--		GameTooltip:Show();
--		DEFAULT_CHAT_FRAME:AddMessage("xxx");
--	end
end


function vyCardInfo_OnLoad(this)
	local str = "vyCardInfo "..vyVer.." by Vaily loaded.";
	DEFAULT_CHAT_FRAME:AddMessage(str);
	vyParseCards();
end

-- Thx to: http://lua-users.org/lists/lua-l/2004-09/msg00054.html
function dec2hex(IN)
	local B,K,OUT,I,D=16,"0123456789abcdef","",0
	while IN>0 do
		I=I+1
		IN,D=math.floor(IN/B),math.mod(IN,B)+1
		OUT=string.sub(K,D,D)..OUT
	end
	return OUT
end

local SetAuctionBrowseItem_orig = GameTooltip_obj["SetAuctionBrowseItem"];
function GameTooltip:SetAuctionBrowseItem(id)
	SetAuctionBrowseItem_orig(GameTooltip_obj, id);
	vyCardCheck(self, GetAuctionBrowseItemLink(id));
end

local SetBagItem_orig = GameTooltip_obj["SetBagItem"];
function GameTooltip:SetBagItem(id)
	SetBagItem_orig (GameTooltip_obj, id);
	vyCardCheck(self, GetBagItemLink(id));
end

local SetBankItem_orig = GameTooltip_obj["SetBankItem"];
function GameTooltip:SetBankItem(id)
	SetBankItem_orig(GameTooltip_obj, id);
	vyCardCheck(self, GetBankItemLink(id));
end

local SetBootyItem_orig = GameTooltip_obj['SetBootyItem'];
function GameTooltip:SetBootyItem(id)
	SetBootyItem_orig(GameTooltip_obj, id);
	vyCardCheck(self, GetBootyItemLink(id));
end


-- Inventory Viewer 1.4+
-- thx to: Zilvermoon
if (GameTooltip_obj["SetHouseItem"]) then
	local SetHouseItem_orig = GameTooltip_obj["SetHouseItem"];
	function GameTooltip:SetHouseItem(ParentDBID,ButtonId)
		SetHouseItem_orig(GameTooltip_obj,ParentDBID,ButtonId);
		vyCardCheck(self, Houses_GetItemLink(ParentDBID,ButtonId));
	end

	local SetStoreItem_orig = GameTooltip_obj["SetStoreItem"];
	function GameTooltip:SetStoreItem(tab,ButtonId)
		SetStoreItem_orig(GameTooltip_obj,tab,ButtonId);
		if (tab == "SELL") then
			vyCardCheck(self,GetStoreSellItemLink(ButtonId));
		elseif (tab == "BUYBACK") then
			vyCardCheck(self,GetStoreBuyBackItemLink(ButtonId));
		end
	end

	local IVSetHyperLink_orig = GameTooltip_obj["SetHyperLink"];
	function GameTooltip:SetHyperLink(link)
		IVSetHyperLink_orig(GameTooltip_obj, link);
		vyCardCheck(self, link);
	end
end

local SetHyperLink_orig = GameTooltipHyperLink_obj["SetHyperLink"];
function GameTooltipHyperLink:SetHyperLink(link)
	SetHyperLink_orig(GameTooltipHyperLink_obj, link);
	vyCardCheck(self, link);
end

-- Advanced Auctionhouse 1.2+
-- thx to: Graves
if(AA_Tooltip) then
	local AA_Tooltip_obj = AA_Tooltip;
	local SetHyperLink_orig = AA_Tooltip_obj["SetHyperLink"];
	function AA_Tooltip:SetHyperLink(link)
		SetHyperLink_orig(AA_Tooltip_obj, link);
		vyCardCheck(self, link);
	end
end